﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StudentHookups.WCF;
using System.Web.Security;
using ClassLibrary;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StudentHookups.Account.Messages
{
    public partial class View_Messages : System.Web.UI.Page
    {
        WCFClient client = new WCFClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.IsInRole("Premium"))
            {
                //Fix to handle messages without spaces
                displayResults.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");

                List<MessageComposite> messages = client.getMessages((Guid)Membership.GetUser().ProviderUserKey);
                List<DisplayMessage> messagesToShow = new List<DisplayMessage>();

                foreach (MessageComposite msg in messages)
                {
                    DisplayMessage message = new DisplayMessage(
                        Membership.GetUser(msg.Sender).UserName,
                        Membership.GetUser(msg.Recepient).UserName,
                        msg.Message,
                        msg.Viewed,
                        msg.Sentdate);

                    messagesToShow.Add(message);
                }


                displayResults.DataSource = messagesToShow;
                displayResults.DataBind();
            }
            else
            {
                lblPageInfo.Text = "You are not a premium user and are not allowed to view your incoming messages";
            }
               
        }
    }
}